﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib;
using Microsoft.Office.Interop.Excel;
using WFNetLib.Log;

namespace PassportManage
{
    public partial class Form_Statistics : Form
    {
        DateTime dt;
        int StatType;
        public Form_Statistics()
        {
            InitializeComponent();
            WFNetLib.MyControls.ToolStripDateTimePicker startDateTimePicker = new WFNetLib.MyControls.ToolStripDateTimePicker(DateTime.Now.AddDays(-7));
            startDateTimePicker.Control.Width = 120;
            toolStrip1.Items.Insert(2, startDateTimePicker);
            toolStripComboBox1.SelectedIndex = 0;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            DateTime start;
            start = ((WFNetLib.MyControls.ToolStripDateTimePicker)toolStrip1.Items[2]).Control.Value;
            if (toolStripComboBox1.SelectedIndex == 0)//按日
            {
                systemLogTableAdapter1.Fill_GetSave(this.passportManageDataSet1.SystemLog, start.Date, start.AddDays(1).Date);
                systemLogTableAdapter1.Fill_GetOut(this.passportManageDataSet2.SystemLog, start.Date, start.AddDays(1).Date);
            }
            toolStripStatusLabel3.Text = "存入   数量为：" + dataGridView1.Rows.Count;
            toolStripStatusLabel1.Text = "取出   数量为：" + dataGridView2.Rows.Count;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if ((dataGridView1.Rows.Count + dataGridView2.Rows.Count) == 0)
                return;
            ExcelExport ExcelEx;
            StatType = toolStripComboBox1.SelectedIndex;
            dt = ((WFNetLib.MyControls.ToolStripDateTimePicker)toolStrip1.Items[2]).Control.Value;
            ExcelEx = new ExcelExport(dataGridView1.Rows.Count + dataGridView2.Rows.Count);
            ExcelEx.ExcelWorkbookCallbackProc = new ExcelExport.ExcelWorkbookCallback(ExcelWorkbookCallbackProc);
            ExcelEx.ExcelExportProc();

        }
        private void ExcelWorkbookCallbackProc(Workbook wBook, int index)
        {
            Range dr;
            if (index == -1)
                return;
            if (index < dataGridView1.Rows.Count)//存入的
            {
                Worksheet wSheet = (Worksheet)wBook.Worksheets[wBook.Worksheets.Count];
                if (index == 0)
                {
                    wSheet.Name = "存入";
                    if (StatType == 0)//按日
                    {
                        wSheet.Cells[1, 1] = "按日存入情况:"+dt.Date.ToString("yyyy-MM-dd");
                        dr = wSheet.get_Range("A1", "E1");
                        dr.Merge(System.Reflection.Missing.Value);                        
                        dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlanchedAlmond);
                        dr.Interior.Pattern = XlPattern.xlPatternSolid;
                        wSheet.Cells[2, 1] = "编号";
                        wSheet.Cells[2, 2] = "内容";
                        wSheet.Cells[2, 3] = "形式";
                        wSheet.Cells[2, 4] = "操作员";
                        wSheet.Cells[2, 5] = "时间";
                        dr = wSheet.get_Range("A2", "E2");
                        dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkOrange);
                        dr.Interior.Pattern = XlPattern.xlPatternSolid;                                             
                    }                    
                }
                SystemLogData log = SystemLogDataFactory.Construct(dataGridView1, index);
                wSheet.Cells[3 + index, 1] = log.ID.ToString();
                wSheet.Cells[3 + index, 2] = log.LogContent;
                wSheet.Cells[3 + index, 3] = log.LogRemark;
                wSheet.Cells[3 + index, 4] = log.Operator;
                wSheet.Cells[3 + index, 5] = log.AddTime.ToString();
                if (index == dataGridView1.Rows.Count - 1)
                {
                    dr = wSheet.get_Range("A1", "E" + (dataGridView1.Rows.Count + 2).ToString());
                    dr.Columns.AutoFit();
                    dr.HorizontalAlignment = XlHAlign.xlHAlignCenter;
                    dr.Borders.LineStyle = XlLineStyle.xlContinuous;   
                }
            }
            else if (index < (dataGridView1.Rows.Count + dataGridView2.Rows.Count))
            {
                if (dataGridView2.Rows.Count == 0)
                    return;
                if (index == dataGridView1.Rows.Count)
                {
                    Worksheet wSheet1 = (Worksheet)wBook.Worksheets[wBook.Worksheets.Count];
                    wBook.Worksheets.Add(System.Reflection.Missing.Value, wSheet1, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                }
                Worksheet wSheet = (Worksheet)wBook.Worksheets[wBook.Worksheets.Count];
                if (index == dataGridView1.Rows.Count)
                {                    
                    wSheet.Name = "取出";
                    if (StatType == 0)//按日
                    {
                        wSheet.Cells[1, 1] = "按日取出情况:" + dt.Date.ToString("yyyy-MM-dd");
                        dr = wSheet.get_Range("A1", "E1");
                        dr.Merge(System.Reflection.Missing.Value);
                        dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlanchedAlmond);
                        dr.Interior.Pattern = XlPattern.xlPatternSolid;
                        wSheet.Cells[2, 1] = "编号";
                        wSheet.Cells[2, 2] = "内容";
                        wSheet.Cells[2, 3] = "形式";
                        wSheet.Cells[2, 4] = "操作员";
                        wSheet.Cells[2, 5] = "时间";
                        dr = wSheet.get_Range("A2", "E2");
                        dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkOrange);
                        dr.Interior.Pattern = XlPattern.xlPatternSolid;
                    }                    
                }
                SystemLogData log = SystemLogDataFactory.Construct(dataGridView2, index - dataGridView1.Rows.Count);
                wSheet.Cells[3 + index - dataGridView1.Rows.Count, 1] = log.ID.ToString();
                wSheet.Cells[3 + index - dataGridView1.Rows.Count, 2] = log.LogContent;
                wSheet.Cells[3 + index - dataGridView1.Rows.Count, 3] = log.LogRemark;
                wSheet.Cells[3 + index - dataGridView1.Rows.Count, 4] = log.Operator;
                wSheet.Cells[3 + index - dataGridView1.Rows.Count, 5] = log.AddTime.ToString();
                if (index == dataGridView1.Rows.Count + dataGridView2.Rows.Count - 1)
                {
                    dr = wSheet.get_Range("A1", "E" + (dataGridView2.Rows.Count + 2).ToString());
                    dr.Columns.AutoFit();
                    dr.HorizontalAlignment = XlHAlign.xlHAlignCenter;
                    dr.Borders.LineStyle = XlLineStyle.xlContinuous;
                }
            }
            
            
//             if (index == -1)
//             {
//                 wSheet.Name = "系统日志";
//                 wSheet.Cells[1, 1] = "编号";
//                 wSheet.Cells[1, 2] = "类型";
//                 wSheet.Cells[1, 3] = "内容";
//                 wSheet.Cells[1, 4] = "备注";
//                 wSheet.Cells[1, 5] = "操作员";
//                 wSheet.Cells[1, 6] = "时间";
//                 dr = wSheet.get_Range("A1", "F1");
//                 dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkOrange);
//                 dr.Interior.Pattern = XlPattern.xlPatternSolid;
//             }
//             else if (index == int.MaxValue)
//             {
//                 dr = wSheet.get_Range("A1", "F" + (dgv.Rows.Count + 1).ToString());
//                 dr.Columns.AutoFit();
//                 dr.HorizontalAlignment = XlHAlign.xlHAlignCenter;
//                 dr.Borders.LineStyle = XlLineStyle.xlContinuous;
//             }
//             else
//             {
//                 SystemLogData log = SystemLogDataFactory.Construct(dgv, index);
//                 wSheet.Cells[2 + index, 1] = log.ID.ToString();
//                 wSheet.Cells[2 + index, 2] = log.LogType;
//                 wSheet.Cells[2 + index, 3] = log.LogContent;
//                 wSheet.Cells[2 + index, 4] = log.LogRemark;
//                 wSheet.Cells[2 + index, 5] = log.Operator;
//                 wSheet.Cells[2 + index, 6] = log.AddTime.ToString();
//                 if (index % 2 == 1)
//                 {
//                     dr = wSheet.get_Range("A" + (2 + index).ToString(), "F" + (2 + index).ToString());
//                     dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
//                     dr.Interior.Pattern = XlPattern.xlPatternSolid;
//                 }
//             }  
        }
    }
}
